package com.beidouin.GnssMain.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

import com.beidouin.GnssMain.models.PointList;
import com.beidouin.GnssMain.R;

/**
 * Created by flyingfluff on 2016/9/23.
 */

public class PvtView extends View {

    private int colorMain, colorBkground;
    private Paint paintWhite, paintBlack, paintGray1, paintGray2, paintPoint1, paintPoint2, paintBkground;

    private int[] range = {2, 10, 30, 100, 500, 2000, 10000, 50000, 200000, 1000000};
    private int curRange = 0;

    private double[][] diffXY = new double[PointList.pointNumMax][2];
    private double[] distance = new double[PointList.pointNumMax];
    private double[] newDiffXY = new double[2];
    private int diffNum = 0;

    public PvtView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context) {
        if (isInEditMode()) { return; }

        colorMain = context.getResources().getColor(R.color.app_main);
        colorBkground = context.getResources().getColor(R.color.activity_background);

        paintWhite = new Paint();
        paintWhite.setColor(Color.WHITE);
        paintWhite.setStyle(Paint.Style.FILL_AND_STROKE);
        paintWhite.setAntiAlias(true);

        paintBlack = new Paint();
        paintBlack.setColor(Color.BLACK);
        paintBlack.setStyle(Paint.Style.FILL_AND_STROKE);
        paintBlack.setAntiAlias(true);

        paintGray1 = new Paint();
        paintGray1.setColor(Color.GRAY);
        paintGray1.setStyle(Paint.Style.STROKE);
        paintGray1.setAntiAlias(true);
        paintGray1.setTextSize(20);//设置字体大小

        paintGray2 = new Paint();
        paintGray2.setColor(Color.GRAY);
        paintGray2.setStyle(Paint.Style.FILL_AND_STROKE);
        paintGray2.setAntiAlias(true);
        paintGray2.setTextSize(20);//设置字体大小

        paintPoint1 = new Paint();
        paintPoint1.setColor(Color.GRAY);
        paintPoint1.setStyle(Paint.Style.FILL_AND_STROKE);
        paintPoint1.setAntiAlias(true);
        paintPoint1.setStrokeWidth(5.0f);

        paintPoint2 = new Paint();
        paintPoint2.setColor(colorMain);
        paintPoint2.setStyle(Paint.Style.FILL_AND_STROKE);
        paintPoint2.setAntiAlias(true);
        paintPoint2.setStrokeWidth(5.0f);

        paintBkground = new Paint();
        paintBkground.setColor(colorBkground);
        paintBkground.setStyle(Paint.Style.FILL_AND_STROKE);
        paintBkground.setAntiAlias(true);
    }

    public void updateView() {
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {

        PreDraw();
        DrawPvtFrame(canvas);
        DrawPvtPoint(canvas);

        super.onDraw(canvas);
    }

    /*
    @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            setMeasuredDimension(measureWidth(widthMeasureSpec),
                    measureWidth(heightMeasureSpec));
        }

        public int measureWidth(int measureSpec) {
            int result = 0;
            int specMode = MeasureSpec.getMode(measureSpec);
            int specSize = MeasureSpec.getSize(measureSpec);
            if (specMode == MeasureSpec.EXACTLY) {
                result = specSize;
            } else {
                result = 200;
                if (specMode == MeasureSpec.AT_MOST) {
                    result = Math.min(specSize, result);
                }
            }
            return result;
        }
     */

    private void PreDraw() {
        if (PointList.getInstance().getPointNum() == 0) {
            curRange = 0;
            diffNum = 0;
            return;
        }

        diffNum = PointList.getInstance().getListLength();
        int cur = PointList.getInstance().getCurrentPos();

        //save points data
        for (int i = 0; i < diffNum; ++i) {
            diffXY[i] = PointList.getInstance().getDiffXY(i);
            distance[i] = Math.sqrt(Math.pow(diffXY[i][0], 2.0) + Math.pow(diffXY[i][1], 2.0));
        }
        //save current point data
        newDiffXY = PointList.getInstance().getDiffXY(cur);

        //save distance to center point
        double max = distance[0];
        for(int i=1; i<diffNum; i++) {
            if(distance[i] > max)
                max = distance[i];
        }

        //get the max distance and pvt range
        for (curRange=0; curRange<range.length; ++curRange) {
            if (max < range[curRange])
                break;
        }
        if (curRange == range.length)
            --curRange;
    }

    private void DrawPvtFrame(Canvas canvas) {
        if (isInEditMode()) { return; }

        float center = (float)canvas.getWidth() / 2;
        float radius1 = center;
        Rect rect = new Rect(0, 0, canvas.getWidth(), canvas.getWidth()+5);

        canvas.drawRect(rect, paintBkground);
        canvas.drawCircle(center, center, radius1, paintWhite); //paint circle white
        canvas.drawCircle(center, center, radius1, paintGray1);

        float radius2 = radius1 / 2;
        canvas.drawCircle(center, center, radius2, paintGray1);

        //horizontal line
        canvas.drawLine(0, center, center*2, center, paintGray1);
        //vertical line
        canvas.drawLine(center, 0, center, center*2, paintGray1);

        String str = String.valueOf(range[curRange]) + "m";
        canvas.drawText("N", center + 2f, 23f, paintGray2);
        canvas.drawText(str, 3, center - 2f, paintGray2);
    }

    public void DrawPvtPoint(Canvas canvas) {
        float center = (float)canvas.getWidth() / 2;

        if (PointList.getInstance().getPointNum() == 0)
            return;

        float x, y;
        double r = (double)range[curRange];
        for (int i = 0; i < diffNum; ++i) {
            x = (float)(center + center * diffXY[i][0] / r);
            y = (float)(center + center * diffXY[i][1] / r);
            canvas.drawPoint(x, y, paintPoint1);
        }

        x = (float)(center + center * newDiffXY[0] / r);
        y = (float)(center + center * newDiffXY[1] / r);
        canvas.drawPoint(x, y, paintPoint2);
    }
}
